﻿namespace EyeOpen.OopByTdd.Address
{
	using System.Linq;
	using System.Text;

	public class AddressParser
	{
		public string Parse(string street)
		{
			var stringBuilder = new StringBuilder();

			var index = char.IsDigit(street[0]) ? 0 : street.Length - 1;

			var incr = (index == 0) ? 1 : -1;

			var c = street[index];

			while ((c != ' ') && (c != ','))
			{
				stringBuilder.Append(c);

				index += incr;
				c = street[index];
			}

			var civic = stringBuilder.ToString();

			if (incr == -1)
			{
				civic = new string(civic.Reverse().ToArray());
			}

			return civic;
		}
	}
}